package com.house.health.controller;

import com.house.health.domain.House;
import com.house.health.domain.Page;
import com.house.health.domain.UserHouseData;
import com.house.health.domain.Users;
import com.house.health.service.IHouserService;
import com.house.health.service.IUserService;
import com.house.health.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author DuanChangSuo
 * @date 2023/2/7 21:45
 * @description 类的描述和介绍
 **/
@Controller
public class UserController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IHouserService iHouserService;

    /**
     * 首页点击用户，跳出用户登录或者注册页面，点击登录按钮，填写用户名密码，点击立即登录（校验）【校验成功调转到HomePageController中的toIndexPage页面】
     * @param username
     * @param password
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    @ResponseBody
    public String toCustomerPage(String username,String password,HttpServletRequest req ) throws Exception {
        Users user = new Users();
        user.setuName(username);
        /**
         * 用户登录的时候要对密码加密进行比较
         */
        password = PasswordUtils.encryptBASE64(password.getBytes());
        user.setuPassword(password);
        Users loginUser = iUserService.login(user);
        if(loginUser!=null) {
            req.getSession().setAttribute("loginUser", loginUser);
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 首页首页点击用户，跳出用户登录或者注册页面，点击注册按钮，填写用户名密码，点击立即注册，注册成功
     * @param user
     * @return
     */
    @RequestMapping("/regist")
    @ResponseBody
    public String regist(Users user) {
        int regist;
        try {
            /**
             * 用户注册的时候要将密码加密存储 保护用户的隐私
             */
            String password = PasswordUtils.encryptBASE64(user.getuPassword().getBytes());
            user.setuPassword(password);
            regist = iUserService.regist(user);
            if(regist>0) {
                return "OK";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "FAIL";
    }

    /**
     * 点击个人中心，跳转页面
     * @return
     */
    @RequestMapping("/toUserSystem")
    public String toUserSystemPage() {
        return "customer";
    }

    /**
     * 点击修改我的密码，跳转页面
     * @return
     */
    @RequestMapping("/updatepwd")
    public String toUpdatepwdPage() {
        return "updatepwd";
    }

    /**
     * 点击提交，修改密码
     * @param id
     * @param newPwd
     * @param oldPwd
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateUserPwd")
    @ResponseBody
    public String updateUserPwd(String id,String newPwd,String oldPwd) throws Exception {
        Users oldUser = new Users();
        oldUser.setuID(Integer.parseInt(id));
        /**
         * 用户密码的时候要对密码加密进行比较
         */
        oldPwd = PasswordUtils.encryptBASE64(oldPwd.getBytes());
        oldUser.setuPassword(oldPwd);
        Users checkUser = iUserService.checkOldPwd(oldUser);
        if(checkUser!=null) {
            Users newUser = new Users();
            newUser.setuID(Integer.parseInt(id));
            newPwd = PasswordUtils.encryptBASE64(newPwd.getBytes());
            newUser.setuPassword(newPwd);
            int n = iUserService.updateUserPwd(newUser);
            if(n>0) {
                return "OK";
            }
        }
        return "FAIL";
    }

    /**
     * 点击我发布的信息，跳转页面
     * @return
     */
    @RequestMapping("/toUserRentalPage")
    public String toUserRentalPage() {
        return "myrental";
    }

    /**
     *显示我发布的租房信息
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findUserHouse")
    @ResponseBody
    public UserHouseData houseByUser(HttpServletRequest request, int page, int limit) {
        Page p = new Page();
        Users u = (Users) request.getSession().getAttribute("loginUser");
        String publisher = u.getuNickName();
        p.setPublisher(publisher);
        p.setLimit(limit);
        p.setPage((page - 1) * limit);
        List<House> list = iHouserService.findHouseByUser(p);
        System.out.println(list);
        UserHouseData data = new UserHouseData(0, "200", list.size(), list);
        return data;
    }

    /**
     * 点击发布房源信息列表的修改
     * @param hID
     * @param request
     * @return
     */
    @RequestMapping("/toUpdateHousePage")
    public String toUpdatePage(int hID,HttpServletRequest request) {
        House house = iHouserService.findHouseDetailsById(hID);
        request.getSession().setAttribute("House", house);
        return "updatehouse";
    }

    /**
     * 退出登录，返回到首页
     * @param session
     * @return
     */
    @RequestMapping("/signout")
    public String signout(HttpSession session) {
        session.invalidate();
        return "redirect:toIndexPage";
    }

    @RequestMapping("/welcome")
    public String toWelcomePage() {
        return "welcome";
    }
}